﻿using System;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using Zhang.key;

namespace Zhang.DeskTop
{
    /// <summary>
    /// Verification 的摘要说明
    /// </summary>
    public class Verification : IHttpHandler, IRequiresSessionState
    {
       
        public void ProcessRequest(HttpContext context)
        {
         Random RandomSeed = new Random();
        // 生成随机数 iIlLoO0
        string strWord = "123456789QWERTYUPASDFGHKXCVBNM";

            string NumStr = null;
            for (int i = 0; i < 4; i++)
            {
                NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
            }

            //将验证码保存到session中
            context.Session[SessionKey.UC_USER_LOGIN_VCODE] = NumStr.ToLower();

            CreateImages(context, NumStr);
        }
        private void CreateImages(HttpContext context, string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 18);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            //定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            //定义字体
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Random rand = new Random();
            //随机输出噪点
            for (int i = 0; i < 10; i++)
            {
                int cindex = rand.Next(7);
                Point point1 = new Point(rand.Next(0, image.Width), rand.Next(0,image.Height));
                Point point2 = new Point(rand.Next(0, image.Width), rand.Next(0, image.Height));
                g.DrawLine(new Pen(c[cindex]), point1, point2);
            }
            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);

                Font f = new System.Drawing.Font(font[findex], 15, System.Drawing.FontStyle.Bold);
                Brush b = new System.Drawing.SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 17), ii);
            }
            //画一个边框
            g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, image.Width - 1, image.Height - 1);

            //输出到浏览器
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            //Response.ClearContent();
            context.Response.ContentType = "image/gif";
            context.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}